package com.learn.demo.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

public class FileChannelDemo {
    public static void main(String[] args) throws Exception {
//        write();
//        read();
//        copyByBuffer();
        copyByTransfer();
    }

    private static void copyByTransfer() throws IOException {
        File file = new File("text.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        FileChannel inputStreamChannel = fileInputStream.getChannel();
        FileOutputStream fileOutputStream = new FileOutputStream("text_copy.txt");
        FileChannel outputStreamChannel = fileOutputStream.getChannel();
        outputStreamChannel.transferFrom(inputStreamChannel, 0, inputStreamChannel.size());
        inputStreamChannel.close();
        fileInputStream.close();
        outputStreamChannel.close();
        fileOutputStream.close();
    }

    private static void copyByBuffer() throws IOException {
        File file = new File("text.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        FileChannel inputStreamChannel = fileInputStream.getChannel();
        FileOutputStream fileOutputStream = new FileOutputStream("text_copy.txt");
        FileChannel outputStreamChannel = fileOutputStream.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(8);
        while (true) {
            byteBuffer.clear();
            int read = inputStreamChannel.read(byteBuffer);
            System.out.println("read: " + read);
            if (read == -1) {
                break;
            }
            byteBuffer.flip();
            outputStreamChannel.write(byteBuffer);
        }
        inputStreamChannel.close();
        fileInputStream.close();
        outputStreamChannel.close();
        fileOutputStream.close();
    }

    private static void read() throws IOException {
        File file = new File("text.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        FileChannel fileChannel = fileInputStream.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
        fileChannel.read(byteBuffer);
        byteBuffer.flip();
        String hello = new String(byteBuffer.array(), StandardCharsets.UTF_8);
        System.out.println(hello);
        fileChannel.close();
        fileInputStream.close();
    }

    private static void write() throws IOException {
        String str = "Hello,你好";
        FileOutputStream fileOutputStream = new FileOutputStream("text.txt");
        FileChannel fileChannel = fileOutputStream.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.put(str.getBytes(StandardCharsets.UTF_8));
        byteBuffer.flip();
        fileChannel.write(byteBuffer);
        fileChannel.close();
        fileOutputStream.close();
    }
}
